//
//  WechatUtils.m
//  TimeReport
//
//  Created by roy on 2023/12/4.
//

#import "WechatUtils.h"

@implementation WechatUtils

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(registerApp:(NSString *)appid)
{
  self.appId = appid;
  [WXApi registerApp:appid universalLink:@"https://xiaot.sidlu.com/.well-known/apple-app-site-association"];
}

RCT_EXPORT_METHOD(sendAuthRequest:(NSString *)scope
                  :(NSString *)state
                  :(RCTResponseSenderBlock)callback)
{
  SendAuthReq* req = [[SendAuthReq alloc] init];
  req.scope = scope;
  req.state = state;

  dispatch_async(dispatch_get_main_queue(), ^{
      UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
      [WXApi sendAuthReq:req viewController:rootViewController delegate:self completion:^(BOOL success) {
          callback(@[[NSNumber numberWithBool:success]]);
      }];
  });
  
//  [WXApi sendReq:req completion:^(BOOL success) {
//    callback(@[[NSNumber numberWithBool:success]]);
//  }];

}

- (NSArray<NSString *> *)supportedEvents
{
  return @[RCTWXEventNameWeChatReq, RCTWXEventName];
}

-(void) onReq:(BaseReq*)req
{
  //不需要处理
//    if ([req isKindOfClass:[LaunchFromWXReq class]]) {
//        LaunchFromWXReq *launchReq = req;
//        NSString *appParameter = launchReq.message.messageExt;
//        NSMutableDictionary *body = @{@"errCode":@0}.mutableCopy;
//        body[@"type"] = @"LaunchFromWX.Req";
//        body[@"lang"] =  launchReq.lang;
//        body[@"country"] = launchReq.country;
//        body[@"extMsg"] = appParameter;
//        [self sendEventWithName:RCTWXEventNameWeChatReq body:body];
//    }
}

-(void) onResp:(BaseResp*)resp
{
    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        SendMessageToWXResp *r = (SendMessageToWXResp *)resp;

        NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
        body[@"errStr"] = r.errStr;
        body[@"lang"] = r.lang;
        body[@"country"] =r.country;
        body[@"type"] = @"SendMessageToWX.Resp";
        [self sendEventWithName:RCTWXEventName body:body];
    } else if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *r = (SendAuthResp *)resp;
        NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
        body[@"errStr"] = r.errStr;
        body[@"state"] = r.state;
        body[@"lang"] = r.lang;
        body[@"country"] =r.country;
        body[@"type"] = @"SendAuth.Resp";

        if (resp.errCode == WXSuccess) {
            if (self.appId && r) {
                // ios第一次获取不到appid会卡死，加个判断OK
                [body addEntriesFromDictionary:@{@"appid":self.appId, @"code":r.code}];
              [self sendEventWithName:RCTWXEventName body:body];
            }
        }
        else {
          [self sendEventWithName:RCTWXEventName body:body];
        }
    } else if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *r = (PayResp *)resp;
        NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
        body[@"errStr"] = r.errStr;
        body[@"type"] = @(r.type);
        body[@"returnKey"] =r.returnKey;
        body[@"type"] = @"PayReq.Resp";
        [self sendEventWithName:RCTWXEventName body:body];
    } else if ([resp isKindOfClass:[WXLaunchMiniProgramResp class]]){
        WXLaunchMiniProgramResp *r = (WXLaunchMiniProgramResp *)resp;
        NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
        body[@"errStr"] = r.errStr;
        body[@"extMsg"] = r.extMsg;
        body[@"type"] = @"WXLaunchMiniProgramReq.Resp";
        [self sendEventWithName:RCTWXEventName body:body];

    } else if ([resp isKindOfClass:[WXChooseInvoiceResp class]]){
        WXChooseInvoiceResp *r = (WXChooseInvoiceResp *)resp;
        NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy;
        body[@"errStr"] = r.errStr;
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (WXCardItem* cardItem in r.cardAry) {
            NSMutableDictionary *item = @{@"cardId":cardItem.cardId,@"encryptCode":cardItem.encryptCode,@"appId":cardItem.appID}.mutableCopy;
            [arr addObject:item];
        }
        body[@"cards"] = arr;
        body[@"type"] = @"WXChooseInvoiceResp.Resp";
        [self sendEventWithName:RCTWXEventName body:body];
    }
}

@end
